<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('channel.tabData.createTime')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('channel.ga.packageName')">
          <el-select v-model="searchForm.packageName" :placeholder="$t('channel.prompt.packageName')" style="width: 100%">
            <el-option
              v-for="(item, index) in allPackageData"
              :key="index"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('channel.tabData.name')">
          <el-input v-model="searchForm.channelName" :placeholder="$t('channel.prompt.name')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="$t('channel.tabData.companyName')">
          <el-input v-model="searchForm.company" :placeholder="$t('channel.prompt.companyName')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="primary" @click="add">
        {{ $t('channel.ga.add') }}
      </el-button>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;margin-top: 10px"
      >
        <el-table-column
          fixed="left"
          prop="channelName"
          :label="$t('channel.tabData.name')"
          min-width="150"
        />
        <el-table-column
          fixed="left"
          prop="packageName"
          :label="tableText['packageName']"
          min-width="150"
        />
        <el-table-column
          prop="company"
          :label="$t('channel.tabData.companyName')"
          min-width="150"
        />
        <el-table-column
          prop="googleLink"
          :label="tableText['link']"
          min-width="300"
        />
        <el-table-column
          prop="modifyOperatorName"
          :label="tableText['creator']"
          min-width="120"
        />
        <el-table-column
          prop="createTimeStr"
          :label="tableText['createTime']"
          min-width="150"
        />
        <el-table-column
          :label="$t('common.operate.name')"
          min-width="170"
        >
          <template slot-scope="scope">
            <el-button @click="copyItem(scope.row)">{{ tableText['copy'] }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <channel-ga-dialog
      :show-dialog="editDialog"
      :package-data="allPackageData"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
  </el-row>
</template>

<script>
import { gaChannel, gaChannelExport, gaPackage } from '@/api/operation'
import { formatDate } from '@/utils/method'
import ChannelGaDialog from '@/components/operation/channel-ga-dialog'
export default {
  name: 'ChannelList',
  components: {
    ChannelGaDialog
  },
  data() {
    return {
      loading: false,
      editDialog: false,
      tableData: [],
      allPackageData: [],
      tableText: {},
      time: null,
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('channel.ga')
    this.getData()
    this.getAllPackage()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        startTime: null,
        endTime: null,
        channelName: null,
        packageName: null,
        company: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = null
      this.getData()
    },
    getAllPackage() {
      gaPackage()
        .then(res => {
          if (res.success) {
            this.allPackageData = []
            res.data.forEach(v => {
              this.allPackageData.push(v)
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getData() {
      this.loading = true
      const data = this.searchData()
      gaChannel(data)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    exportList() {
      gaChannelExport(this.searchData())
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['startTime'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['endTime'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['startTime'] = null
        data['endTime'] = null
      }
      return data
    },
    copyItem(data) {
      const input = document.createElement('input')
      document.body.appendChild(input)
      input.setAttribute('value', data)
      input.select()
      if (document.execCommand('copy')) {
        document.execCommand('copy')
        this.$message.success(this.$t('channel.ga.copySuccess'))
      } else {
        this.$message.error(this.$t('channel.ga.copyError'))
      }
      document.body.removeChild(input)
    },
    add() {
      this.editDialog = true
    },
    changeDialog(param) {
      this.editDialog = param
    },
    editSuccess() {
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>
